Imports System
Imports System.Collections.Generic
Imports ASPNET.StarterKit.DataAccessLayer

Namespace ASPNET.StarterKit.BusinessLogicLayer

	Public Class Category

		Private _ActualDuration As Decimal
		Private _Abbreviation As String
		Private _Id As Integer
		Private _EstimateDuration As Decimal
		Private _Name As String
		Private _ProjectId As Integer

		Public Sub New(ByVal categoryName As String, ByVal categoryProjectId As Integer)

			Me.New(String.Empty, _
							 DefaultValues.GetDurationMinValue(), _
							 DefaultValues.GetCategoryIdMinValue(), _
							 DefaultValues.GetDurationMinValue(), _
							 categoryName, _
							 categoryProjectId)
		End Sub

		Public Sub New(ByVal categoryAbbreviation As String, _
								ByVal categoryEstimateDuration As Decimal, _
								ByVal categoryName As String, _
								ByVal categoryProjectId As Integer)

			Me.New(categoryAbbreviation, _
							 DefaultValues.GetDurationMinValue(), _
							 DefaultValues.GetCategoryIdMinValue(), _
							 categoryEstimateDuration, _
							 categoryName, _
							 categoryProjectId)

		End Sub

		Public Sub New(ByVal categoryAbbreviation As String, _
								ByVal categoryActualDuration As Decimal, _
								ByVal categoryId As Integer, _
								ByVal categoryEstimateDuration As Decimal, _
								ByVal categoryName As String, _
								ByVal categoryProjectId As Integer)

			If String.IsNullOrEmpty(categoryName) Then
				Throw New NullReferenceException("name")
			End If

			If categoryProjectId <= DefaultValues.GetProjectIdMinValue() Then
				Throw New ArgumentOutOfRangeException("projectId")
			End If

			Me._Abbreviation = categoryAbbreviation
			Me._ActualDuration = categoryActualDuration
			Me._Id = categoryId
			Me._EstimateDuration = categoryEstimateDuration
			Me._Name = categoryName
			Me._ProjectId = categoryProjectId

		End Sub

		Public ReadOnly Property ActualDuration() As Decimal

			Get
				Return Me._ActualDuration
			End Get

		End Property

		Public Property Abbreviation() As String

			Get
				If String.IsNullOrEmpty(Me._Abbreviation) Then
					Return String.Empty
				Else
					Return Me._Abbreviation
				End If
			End Get

			Set(ByVal value As String)
				Me._Abbreviation = value
			End Set

		End Property

		Public ReadOnly Property Id() As Integer

			Get
				Return Me._Id
			End Get

		End Property

		Public Property EstimateDuration() As Decimal

			Get
				Return Me._EstimateDuration
			End Get

			Set(ByVal value As Decimal)
				Me._EstimateDuration = value
			End Set

		End Property

		Public Property Name() As String

			Get
				If String.IsNullOrEmpty(Me._Name) Then
					Return String.Empty
				Else
					Return Me._Name
				End If
			End Get

			Set(ByVal value As String)
				Me._Name = value
			End Set

		End Property

		Public Property ProjectId() As Integer

			Get
				Return Me._ProjectId
			End Get

			Set(ByVal value As Integer)
				Me._ProjectId = value
			End Set

		End Property

		Public Function Delete() As Boolean

			If Me.Id > DefaultValues.GetCategoryIdMinValue() Then
				Return DataAccessHelper.GetDataAccess.DeleteCategory(Me.Id)
			Else
				Return False
			End If

		End Function

		Public Function Save() As Boolean

			If Id <= DefaultValues.GetCategoryIdMinValue() Then
				Dim TempId As Integer = DataAccessHelper.GetDataAccess().CreateNewCategory(Me)
				If TempId > DefaultValues.GetCategoryIdMinValue() Then
					Me._Id = TempId
					Return True
				Else
					Return False
				End If
			Else
				Return DataAccessHelper.GetDataAccess().UpdateCategory(Me)
			End If

		End Function

        Public Shared Function DeleteCategory(ByVal id As Integer) As Boolean

            If Id <= DefaultValues.GetCategoryIdMinValue() Then
                Throw (New ArgumentOutOfRangeException("Id"))
            End If

            Return DataAccessHelper.GetDataAccess().DeleteCategory(Id)

        End Function


		Public Shared Function GetAllCategories() As List(Of Category)

			Return DataAccessHelper.GetDataAccess().GetAllCategories()

		End Function

		Public Shared Function GetCategoryByCategoryId(ByVal Id As Integer) As Category

			If Id <= DefaultValues.GetCategoryIdMinValue() Then
				Return (Nothing)
			End If

			Return DataAccessHelper.GetDataAccess().GetCategoryByCategoryId(Id)

		End Function

		Public Shared Function GetCategoriesByProjectId(ByVal projectId As Integer) As List(Of Category)

			If projectId <= DefaultValues.GetProjectIdMinValue() Then
				Return New List(Of Category)()
			End If

			Return DataAccessHelper.GetDataAccess().GetCategoriesByProjectId(projectId)

		End Function

		Public Shared Function GetCategoryByCategoryNameandProjectId(ByVal categoryName As String, ByVal projectId As Integer) As Category

			If projectId <= DefaultValues.GetProjectIdMinValue() Or String.IsNullOrEmpty(categoryName) Then
				Return (Nothing)
			End If

			Return DataAccessHelper.GetDataAccess().GetCategoryByCategoryNameandProjectId(categoryName, projectId)

        End Function


		Public Shared Function GetCategoriesByProjectIds(ByVal ProjectIds As String) As List(Of Category)

			If String.IsNullOrEmpty(ProjectIds) Then
				Return New List(Of Category)()
			End If

			Dim separator As Char() = New Char() {","c}
			Dim substrings As String() = ProjectIds.Split(separator)
			Dim list As New List(Of Category)()

			For Each str As String In substrings
				If Not String.IsNullOrEmpty(str) Then
					Dim id As Integer = Convert.ToInt32(str)
					Dim tempList As List(Of Category) = Category.GetCategoriesByProjectId(id)
					For Each category As Category In tempList
						list.Add(category)
					Next category
				End If
			Next str

			Return list

		End Function

		Public Shared Function UpdateCategory(ByVal Abbreviation As String, ByVal originalId As Integer, ByVal EstimateDuration As Decimal, ByVal Name As String) As Boolean

			If String.IsNullOrEmpty(Name) Then
				Throw New NullReferenceException("Name")
			End If

			Dim updateCategoryObj As Category = Category.GetCategoryByCategoryId(originalId)
			If Not updateCategoryObj Is Nothing Then
				updateCategoryObj.Abbreviation = Abbreviation
				updateCategoryObj.EstimateDuration = EstimateDuration
				updateCategoryObj.Name = Name
				Return updateCategoryObj.Save()
			Else
				Return False
			End If

		End Function

	End Class

End Namespace
